package com.hm.scala

import java.util.Date
import java.text.SimpleDateFormat

object Func {
  def main(args: Array[String]): Unit = {

    println(addInt(3, 2));

    println("-------华丽分割线---");
    returnVoid("兽兽")

    println("-------华丽分割线---");

    println(calculate(19, 9));

    println("-------华丽分割线---");

    //代码写在一行

    def line(str: String, a: Int) = str + a;
    println(line("shishun", 156))

    var value = (x: Int, y: Int) => {
      (x + y) * x + y
    };

    println("-------华丽分割线---");
    println(value(1, 2));

    println("-------华丽分割线（闭包）---");

    def add(more: Int) = (x: Int) => x + more

    var a = add(1);
    print(a(10));

    println("-------华丽分割线（闭包）---");
    def add2(more: Int) = {

      println("------->" + more)
      (number: Int) => more + number

    }

    var v = add2(1);
    print(v(10))

    println("-------华丽分割线（含有默认参数）---");

    defaultParam("soso", new Date());

    println("-------华丽分割线（多参数）---");

    moreParam("aa", "bb", "ccc")

    println("-------华丽分割线（指定参数）---");

    try {
      defaultParam(age = 23, str = "王明", birth = new Date());
    } catch {
      case t: Throwable => t.printStackTrace() // TODO: handle error
      case e: Exception => println(e.getMessage)
    } finally {
      println("程序运行结束")
    }
  }

  def moreParam(str: String*) {
    str.foreach(s => println(s));
  }

  def defaultParam(str: String, birth: Date, age: Int = 15) {
    println(str + "今年" + age + "岁,创建日期" + new SimpleDateFormat("yyyy-MM-dd").format(birth));
  }

  /**
   * 有参方法定义
   * @param a
   * @param b
   * @return
   */
  def addInt(a: Int, b: Int): Int = {
    var total: Int = a * b;
    return total;
  }

  /**
   * void方法定义
   * @param str
   */
  def returnVoid(str: String): Unit = {
    println("你好！" + str);
  }

  /**
   * 不写返回值让程序自行判断
   * @param a
   * @param b
   * @return
   */
  def calculate(a: Int, b: Int) = {
    a - b;
  }
}